package keer.httpinterface;

import keer.httpinterface.http.HttpProperties;
import keer.httpinterface.http.HttpRequester;
import keer.httpinterface.http.URLHttpRequester;
import keer.httpinterface.proxy.JdkHttpInterfaceProxyFactory;
import keer.httpinterface.proxy.ProxyFactory;
import keer.httpinterface.utils.HttpInterfaceFactories;

/**
 * HTTP接口构建器类，用于根据配置属性构建HTTP请求接口的代理实例
 */
public class HttpInterfaceBuilder {
    // HTTP配置属性
    private final HttpProperties properties;
    // HTTP请求执行器
    private HttpRequester requester;
    // 代理工厂，用于创建代理对象
    private ProxyFactory proxyFactory;

    /**
     * 私有构造函数，用于创建HttpInterfaceBuilder实例
     *
     * @param properties HTTP配置属性
     * @param classLoader 类加载器
     * @throws HttpInterfaceException 当实例化过程中发生错误时抛出
     */
    private HttpInterfaceBuilder(HttpProperties properties, ClassLoader classLoader) throws HttpInterfaceException {
        this.properties = properties;
        requester = HttpRequester.getInstance(classLoader);
        requester.configure(properties);
        proxyFactory = ProxyFactory.getInstance(classLoader);
    }

    /**
     * 静态工厂方法，创建HttpInterfaceBuilder实例，使用默认的HttpProperties
     *
     * @return 新的HttpInterfaceBuilder实例
     * @throws HttpInterfaceException 当创建过程中发生错误时抛出
     */
    public static HttpInterfaceBuilder create() throws HttpInterfaceException {
        return create(new HttpProperties());
    }

    /**
     * 静态工厂方法，创建HttpInterfaceBuilder实例，允许指定HttpProperties
     *
     * @param properties HTTP配置属性
     * @return 新的HttpInterfaceBuilder实例
     * @throws HttpInterfaceException 当创建过程中发生错误时抛出
     */
    public static HttpInterfaceBuilder create(HttpProperties properties) throws HttpInterfaceException {
        return create(properties, Thread.currentThread().getContextClassLoader());
    }

    /**
     * 静态工厂方法，创建HttpInterfaceBuilder实例，允许指定HttpProperties和ClassLoader
     *
     * @param properties HTTP配置属性
     * @param classLoader 类加载器
     * @return 新的HttpInterfaceBuilder实例
     * @throws HttpInterfaceException 当创建过程中发生错误时抛出
     */
    public static HttpInterfaceBuilder create(HttpProperties properties, ClassLoader classLoader) throws HttpInterfaceException {
        return new HttpInterfaceBuilder(properties, classLoader);
    }

    /**
     * 设置HTTP请求执行器
     *
     * @param requester HTTP请求执行器
     * @return 当前的HttpInterfaceBuilder实例，支持链式调用
     * @throws HttpInterfaceException 当配置请求执行器过程中发生错误时抛出
     */
    public HttpInterfaceBuilder requester(HttpRequester requester) throws HttpInterfaceException{
        this.requester = requester;
        this.requester.configure(this.properties);
        return this;
    }

    /**
     * 设置代理工厂
     *
     * @param proxyFactory 代理工厂
     * @return 当前的HttpInterfaceBuilder实例，支持链式调用
     */
    public HttpInterfaceBuilder proxyFactory(ProxyFactory proxyFactory) {
        this.proxyFactory = proxyFactory;
        return this;
    }

    /**
     * 构建HTTP接口的代理实例
     *
     * @param targetInterface 目标接口类型
     * @return 目标接口的代理实例
     * @throws HttpInterfaceException 当构建过程中发生错误时抛出
     */
    public <T> T build(Class<T> targetInterface) throws HttpInterfaceException{
        return proxyFactory.createProxy(targetInterface, requester);
    }
}
